<template>
  <div>
    <div class="box-card">
      <div class="box-header">
        <div class="header-top">
          <el-button
            type="success"
            class="header-right"
            @click="openDialog('add')"
            ><i class="el-icon-edit"></i> 新增用户</el-button
          >
        </div>
      </div>
      <!-- 表格 -->
      <el-table
        label-width="150px"
        :data="tableData"
        style="width: 100%"
        :header-cell-style="{ background: '#f4f4f5' }"
        default-expand-all
        row-key="id"
        :tree-props="{ children: 'childs' }"
      >
        <el-table-column prop="title" label="标题" width="300px">
          <template slot-scope="scope">
            <i class="el-icon-document-add"></i>
            <span style="margin-left: 10px">{{ scope.row.title }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="code" label="权限点代码" min-width="200px">
        </el-table-column>
        <el-table-column label="操作" width="200px" align="center">
          <template #default="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              class="edit1"
              @click="openDialog('edit', row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              class="edit2"
              @click="open(row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹出框 -->
    <menusAdd
      :pageTitle="diologTitle"
      :dialogFormVisible.sync="dialogFormVisible"
      ref="menusAdd"
      @getInit="initTable"
    ></menusAdd>
  </div>
</template>

<script>
import { list, remove } from '@/api/base/menus.js'
import menusAdd from '../components/menu-add.vue'
export default {
  components: {
    menusAdd
  },
  data () {
    return {
      input: '',
      tableData: [],
      page: 1,
      pagesize: 10,
      total: 0,
      data: [
        {
          label: '一级 1',
          children: [
            {
              label: '二级 1-1',
              children: [
                {
                  label: '三级 1-1-1'
                }
              ]
            }
          ]
        }
      ],
      dialogFormVisible: false,
      titleType: 'add'
    }
  },

  async created () {
    this.initTable()
  },

  methods: {
    async initTable () {
      const { data } = await list({})
      console.log(data)
      this.tableData = JSON.parse(
        JSON.stringify(data).replace('points', 'childs')
      )
    },
    // 对话框取消按钮
    closeDialog () {
      // TODO弹框的内容还没完成
      this.clearForm()
      this.$refs.form.resetFields()
      this.dialogFormVisible = false
    },

    // 点击新增用户按钮
    async openDialog (type, id) {
      // this.titleType = type
      // console.log(id)
      if (type === 'edit') {
        this.$refs.menusAdd.hanldeEditForm(id)
      }
      this.dialogFormVisible = true
    },
    // 删除 取消确认按钮
    open (id) {
      this.$confirm('此操作将永久删除用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 发删除请求
          await remove({ id })
          this.initTable()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作！'
          })
        })
    }
  },
  computed: {
    diologTitle () {
      if (this.titleType === 'add') {
        return '创建菜单'
      } else if (this.titleType === 'edit') {
        return '创建菜单'
      } else {
        return '创建菜单'
      }
    }
  }
}
</script>

<style scoped lang="less">
.box-card {
  margin: 20px;
  background-color: #fff;
  padding: 20px;
  .box-header {
    .header-top {
      height: 45px;
      margin-bottom: 10px;
      .header-left {
        display: flex;
        .header-btn {
          width: 60px;
          font-size: 12px;
        }
      }
      .header-right {
        width: 90px;
        height: 32px;
        font-size: 12px;
        float: right;
        .el-icon-edit {
          margin-left: -6px;
        }
      }
    }
  }
  .edit1 {
    color: #409eff;
    background: #ecf5ff;
    border-color: #b3d8ff;
    &:hover {
      background-color: #409eff;
      color: #fff;
    }
  }
  .edit2 {
    color: #f56c6c;
    background: #fef0f0;
    border-color: #fbc4c4;
    &:hover {
      background-color: #f56c6c;
      color: #fff;
    }
  }
  .el-pagination {
    height: 40px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding-top: 20px;
  }
}

.el-dialog {
  .el-form {
    width: 400px;
    margin: auto;
    .el-input {
      width: 310px;
      height: 35px;
    }
  }
  .dialog-footer {
    margin-top: 60px;
    text-align: center;
  }
}
</style>
